package cn.ydt.ydt.community.webapi.controller;
import cn.ydt.ydt.community.common.restful.JsonPage;
import cn.ydt.ydt.community.common.utils.WebConsts;
import cn.ydt.ydt.community.pojo.dto.ModifySingInInfoDTO;
import cn.ydt.ydt.community.pojo.vo.SignInfoVO;
import cn.ydt.ydt.community.service.EnclosureSingInLocationService;
import cn.ydt.ydt.common.resful.JsonResult;
import cn.ydt.ydt.common.resful.StatusCode;
import cn.ydt.ydt.community.pojo.dto.EnclosureSingInLocationDTO;
import cn.ydt.ydt.community.pojo.dto.SingInDTO;
import cn.ydt.ydt.community.service.SingInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 *处理签到请求
 * @author Administrator
 */
@RestController
@RequestMapping("/community/location")
@Slf4j(topic = "community.location")
@Api(tags = "用户签到相关功能")
public class LocationController {

    private static final String IN_CLOSURE = "签到成功!";
    private static final String OUT_CLOSURE= "签到成功，未在签到范围内!";

    @Autowired
    EnclosureSingInLocationService enclosureService;

    @Autowired
    SingInService singInService;



    @ApiOperation(value = "用户签到")
    @PostMapping("/sing-in")
    public JsonResult singIn( @Valid  @RequestBody SingInDTO singInDTO) {
        log.info("签到信息：{}",singInDTO.toString());
        String message = singInService.addSingInRecord(singInDTO);
        return  JsonResult.ok(StatusCode.OK , message);

    }


    /**
     *处理前端发送的打卡范围参数
     * @return
     */
    @ApiOperation(value = "划分打卡区域")
    @PostMapping("/enclosure")
    public JsonResult enclosureSingInLocation(@Valid @RequestBody EnclosureSingInLocationDTO enclosureDTO){
        log.info("划定打卡信息：{}" , enclosureDTO.toString());
        enclosureService.addEnclosure(enclosureDTO);
        return  JsonResult.ok(StatusCode.OK , "新增打卡区域成功");
    }


    /**
     * 以社区为单位获得用户某天的的打卡信息
     * @param page
     * @param pageSize
     * @param difference
     * @return
     */
    @ApiOperation(value = "以社区为单位获得用户当天的打卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",dataType = "int",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",
                    dataType = "int",example = "5"),
            @ApiImplicitParam(value = "查询日期相隔天数(-1为昨天，-2为前天，以此类推)",name = "difference",
                    dataType = "int",example = "-1" )

    })
    @GetMapping("/list-sign-in-info")
    public JsonResult<JsonPage<SignInfoVO>> getCurrentSingInInfo( @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                                                                              Integer page,
                                                                  @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                                                                              Integer pageSize,
                                                                  @RequestParam(required = false, defaultValue = WebConsts.DEFAULT_DIFFERENCE)
                                                                              Integer difference){
        JsonPage<SignInfoVO> info = singInService.getCurrentSignInInfoByCommunityId(page, pageSize,difference);
        return JsonResult.ok(info) ;
    }


    /**
     * 获取用户的签到记录
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取用户的签到记录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",dataType = "int",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize", dataType = "int",example = "5"),

    })

    @GetMapping("/list-person-sign-info")
    public JsonResult<JsonPage<SignInfoVO>> getPersonSignInInfo( @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                                                                         Integer page,
                                                             @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                                                                         Integer pageSize){
        JsonPage<SignInfoVO> info = singInService.getPersonSignInInfo(page ,pageSize);
        return JsonResult.ok(info);
    }



    @GetMapping("/modify-sign-in-info")
    @ApiOperation(value = "修改用户的签到信息")
    public JsonResult modifySingInInfo(@Valid @RequestBody ModifySingInInfoDTO modify){
       singInService.modifySingInInfo(modify);
       return JsonResult.ok();

    }


}
